package cc.orange.base;

import android.animation.AnimatorSet;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import cc.orange.R;
import cc.orange.utils.ZXToastUtil;
import demo.smart.access.xutlis.util.ZXSharedPrefUtil;

public class BaseFragment extends Fragment {

    private Toast toast;
    private AlertDialog dialog;
    private ImageView ivLoading;
    private AnimationDrawable animationDrawable;
    private long lastClickTime;
    private AlertDialog dialog_head;
    private ImageView ivLoading_head;
    private AnimatorSet animatorSet;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_base, container, false);
    }


    public void showToast(String msg) {
//        cancelToast();
//        if (toast == null) {
//            toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
//        }
//        toast.show();
        ZXToastUtil.showToast(msg);
    }

    public void cancelToast() {
        if (toast != null) {
            toast.cancel();
            toast = null;
        }
    }

    public void showLoading_base(Context context) {
        if ((dialog == null || ivLoading == null) && null != context) {
            View view = LayoutInflater.from(context).inflate(R.layout.layout_loading_lootom, null, false);
            ivLoading = (ImageView) view.findViewById(R.id.iv_loading);
            dialog = new AlertDialog.Builder(context, R.style.dialog_transparent).create();
            dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    if (ivLoading != null) {
                        ivLoading.clearAnimation();
                    }
                }
            });
            dialog.setCancelable(true);
            dialog.setView(view);
        }

        animationDrawable = (AnimationDrawable) ivLoading.getDrawable();
        //开始动画
        animationDrawable.start();

        if (!dialog.isShowing()) {
            dialog.show();
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                cancelLoading();
            }
        }, 5000);
    }

    public void cancelLoading() {
        if (dialog != null && dialog.isShowing()) {
//            if (ivLoading != null) {
//                ivLoading.clearAnimation();
//            }
            if (animationDrawable != null) {
                animationDrawable.stop();
            }
            dialog.dismiss();
        }
    }

    /**
     * 为防止多次重复点击
     *
     * @return
     */
    public synchronized boolean isFastClick() {
        long time = System.currentTimeMillis();
        if (time - lastClickTime < 500) {
            return true;
        }
        lastClickTime = time;
        return false;
    }

    public String getTokens(){
        ZXSharedPrefUtil sh = new ZXSharedPrefUtil();
        return sh.getString("token_talk","");
    }
}